package com.rustic.server.controller.admapi;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.xy.base.server.utils.PageUtil;
import com.rustic.server.query.RestaurantAdminListQuery;
import com.rustic.server.dto.RestaurantAddDTO;
import com.rustic.server.dto.RestaurantUpdateDTO;
import com.rustic.server.po.Restaurant;
import com.rustic.server.service.RestaurantService;
import com.rustic.server.vo.list.RestaurantAdminListVO;
import com.rustic.server.vo.RestaurantVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import cn.xy.commons.annotation.OperateLog;
import cn.xy.commons.core.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 表[restaurant]对应的后台管理类
 *
 * @author auto
 *
 */

@Slf4j
@RestController
@RequestMapping("/admapi/restaurant")
@Api(tags = {"餐厅管理接口"})
public class RestaurantAdminController {

    @Autowired
    private RestaurantService restaurantService;

    @ApiOperation(value = "餐厅-列表", notes = "权限key: restaurant-list")
    @GetMapping("/list")
    @PreAuthorize("@validate.hasPermission('restaurant-list')")
    public Result<Page<RestaurantAdminListVO>> list(@Validated RestaurantAdminListQuery query) {
        LambdaQueryWrapper<Restaurant> queryWrapper = restaurantService.toAdminListWrapper(query);
        if (queryWrapper == null) {
            return Result.data(new Page<>());
        }
        Page<Restaurant> pageData = restaurantService.page(query.toPage(), queryWrapper);
        List<Restaurant> restaurantList = pageData.getRecords();
        if (CollUtil.isEmpty(restaurantList)) {
            return Result.data(new Page<>());
        }
        List<RestaurantAdminListVO> results = restaurantService.toAdminListVo(restaurantList);
        return Result.data(PageUtil.toPage(pageData, results));
    }

    @ApiOperation(value = "餐厅-详情", notes = "权限key: restaurant-detail")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "餐厅id"),
    })
    @GetMapping("/detail")
    @PreAuthorize("@validate.hasPermission('restaurant-detail')")
    public Result<RestaurantVO> detail(int id) {
        if (id <= 0) {
            return Result.error(RestaurantService.RESTAURANT_NOT_EXIST);
        }
        Restaurant restaurant = restaurantService.getById(id);
        if (restaurant == null) {
            return Result.error(RestaurantService.RESTAURANT_NOT_EXIST);
        }
        RestaurantVO restaurantVO = new RestaurantVO(restaurant);
        return Result.data(restaurantVO);
    }

    @ApiOperation(value = "餐厅-增加", notes = "权限key: restaurant-add")
    @PostMapping("/add")
    @PreAuthorize("@validate.hasPermission('restaurant-add')")
    @OperateLog(description = "增加餐厅")
    public Result<String> add(@RequestBody @Validated RestaurantAddDTO restaurantAddDTO) {
        Restaurant restaurant = restaurantAddDTO.toRestaurant();
        restaurantService.save(restaurant);
        return Result.ok("添加餐厅成功");
    }

    @ApiOperation(value = "餐厅-更新", notes = "权限key: restaurant-update")
    @PutMapping("/update")
    @PreAuthorize("@validate.hasPermission('restaurant-update')")
    @OperateLog(description = "更新餐厅")
    public Result<String> update(@RequestBody @Validated RestaurantUpdateDTO restaurantUpdateDTO) {
//        Restaurant restaurant = restaurantService.getById(restaurantUpdateDTO.getId());
//        if (restaurant == null) {
//            return Result.error(RestaurantService.RESTAURANT_NOT_EXIST);
//        }
//        BeanUtils.copyProperties(restaurantUpdateDTO, restaurant);
//        restaurantService.updateById(restaurant);
        return Result.ok("编辑餐厅成功");
    }

    @ApiOperation(value = "餐厅-删除", notes = "权限key: restaurant-delete")
    @DeleteMapping("/delete")
    @PreAuthorize("@validate.hasPermission('restaurant-delete')")
    @OperateLog(description = "删除餐厅")
    public Result<String> delete(int id) {
        if (id <= 0) {
            return Result.error(RestaurantService.RESTAURANT_NOT_EXIST);
        }
        Restaurant restaurant = restaurantService.getById(id);
        if (restaurant == null) {
            return Result.error(RestaurantService.RESTAURANT_NOT_EXIST);
        }
        restaurantService.removeById(id);
        return Result.ok("删除餐厅成功");
    }
}
